package com.huixuebao.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huixuebao.api.entity.CaptchaEntity;
import com.huixuebao.api.mapper.CaptchaMapper;
import com.huixuebao.api.service.CaptchaService;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class CaptchaServiceImpl implements CaptchaService {

    @Autowired
    CaptchaMapper captchaMapper;
    /**
     * @Title checkCaptcha
     * @Description:校验验证码
     */
    @Override
    public boolean checkCaptcha(String captchaUid, String captchaType, String captchaValue, String phone) {
        /*CaptchaDTO captchaDTO = new CaptchaDTO();
        String createTime = DateUtil.formatDateTime(DateUtil.offset(new Date(), DateField.MINUTE, -5));
        //sysCaptchaDTO.setCaptchaType(captchaType);
        captchaDTO.setCaptchaValue(captchaValue.toUpperCase());
        captchaDTO.setCaptchaUid(captchaUid);
        if (CaptchaUtil.CAPTCHA_TYPE_MOBILE.equals(captchaType)) {
            captchaDTO.setPhone(phone);
        }
        QueryWrapper<CaptchaDTO> queryWrapper = new QueryWrapper<>(captchaDTO);
        queryWrapper.gt("create_time", createTime);
        //aptchaDTO captcha = this.baseMapper.selectOne(queryWrapper);
        //if (captcha != null) {
        //验证码校验之后进行删除，保证验证码只能使用一次（暂）
        //this.removeById(captcha);
        //return true;
        //} else {
        //return false;
        //}*/
       return false;
    }

    @Override
    public void save(CaptchaEntity captchaEntity) {
        captchaMapper.insert(captchaEntity);
    }

    @Override
    public List<CaptchaEntity> get(String phone, String smsCode) {
        String createTime =  DateUtil.formatDateTime(DateUtil.offset(new Date(), DateField.SECOND,-120));
        return captchaMapper.selectList(new LambdaQueryWrapper<CaptchaEntity>()
                .eq(CaptchaEntity::getPhone, phone)
                .eq(CaptchaEntity::getCaptchaValue, smsCode)
                .gt(CaptchaEntity::getCreateTime, createTime));
    }
}
